//
//  RTJAutomaticBidDetailsController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/12.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJAutomaticBidDetailsController.h"
#import "RTJAutomaticBidController.h"

@interface RTJAutomaticBidDetailsController ()
@property (weak, nonatomic) IBOutlet UILabel *titleLab;
@property (weak, nonatomic) IBOutlet UILabel *rateLab;
@property (weak, nonatomic) IBOutlet UILabel *moneyLab;
@property (weak, nonatomic) IBOutlet UISwitch *switchBtn;
@property (weak, nonatomic) IBOutlet UIView *bg_view;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewTop;
@property (weak, nonatomic) IBOutlet UILabel *lab1;
@property (weak, nonatomic) IBOutlet UILabel *lab2;

@end

@implementation RTJAutomaticBidDetailsController

-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden = NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"智能投标";
    
     self.viewTop.constant = screenH == 812 ? 88 : 64;
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"nav_fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];

    _bg_view.layer.cornerRadius = 5;
    _bg_view.layer.shadowColor = [UIColor colorForHex:@"#dbc9cc"].CGColor;
    _bg_view.layer.shadowOffset = CGSizeMake(-1, 1);
    _bg_view.layer.shadowOpacity = 0.5;
    _bg_view.layer.shadowRadius = 5;
    _switchBtn.selected = [RTJUserTool getTheUserModel].user_autobid == 1 ? YES :NO;
    
    NSString *str = @"1、标的开始十分钟后，才会启动智能投标。\n2、投标序列按照开启智能投标的时间先后进行排序。\n3、每个用户每个标仅智能投标一次,可投资金投完之后,排到队尾。\n4、轮到用户投标时没有符合用户条件的标，保留排名。";
    NSString *str1 = @"智能投标是一个根据您的设置条件自动帮您完成受托的工具，您只需要设置借款期限、利率范围等条件，开启后系统即可全天候帮您进行智能投标，为您省时省力。";
    
    _lab1.attributedText = [self getAttributedStringWithString:str lineSpace:0];
    _lab2.attributedText = [self getAttributedStringWithString:str1 lineSpace:0];
    
    [self loadData];
    
}

-(NSAttributedString *)getAttributedStringWithString:(NSString *)string lineSpace:(CGFloat)lineSpace {
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpace; // 调整行间距
    NSRange range = NSMakeRange(0, [string length]);
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
    return attributedString;
}

-(void)back{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)loadData{
    
    [SVProgressHUD  showWithStatus:nil];
    [self.view showEmptyViewWithStr:@"" image:nil callback:nil];

    
    [RTJNetWork asyncRequestWithURL:@"user/auto_bid" params:nil method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [self.view removeEmptyView];

        [SVProgressHUD dismiss];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                NSString *period = [NSString stringWithFormat:@"%@",data[@"data"][@"period"]];
                
                if ([period isEqualToString:@"<null>"]) {
                    
                    self.titleLab.text = [NSString stringWithFormat:@"智能投标"];
                }else{
                    
                    self.titleLab.text = [NSString stringWithFormat:@"智能投标%@ 个月",data[@"data"][@"period"]];
                }
                
                
                NSString *str = @"%";
                self.rateLab.text = [NSString stringWithFormat:@"%@%@ ～ %@%@",data[@"data"][@"min_rate"],str,data[@"data"][@"max_rate"],str];
                self.moneyLab.text = [NSString stringWithFormat:@"%@元",data[@"data"][@"retain_amount"]];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                [self.view showEmptyViewWithStr:data[@"msg"] image:[UIImage imageNamed:@"kbylogo"] callback:^{
                    [self loadData];
                }];
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];
            [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                [self loadData];
            }];

        }

        
    }];
}

- (IBAction)switch:(UISwitch *)sender {

    
    sender.selected =!sender.selected;
    
    [SVProgressHUD  showWithStatus:nil];
    
    [RTJNetWork asyncRequestWithURL:@"user/cancel_auto_bid" params:@{@"is_effect":sender.selected == YES ? @"1":@"2"} method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                RTJUserModel *model = [RTJUserTool getTheUserModel];
                
                if(sender.selected == YES){
                    
                    [SVProgressHUD showSuccessWithStatus:@"已开启智能投标功能"];
                    model.user_autobid = 1;
                    
                    
                }else{
                    
                    [SVProgressHUD showSuccessWithStatus:@"已关闭智能投标功能"];
                    model.user_autobid = 0;
                    
                }
                
                [RTJUserTool saveUserInfo:model];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];

        }
        
        
    }];


}

- (IBAction)autobin:(id)sender {
    
    RTJAutomaticBidController *vc = [[RTJAutomaticBidController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
